package com.datastructure.test.lru;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    private Node[] nodes;
    private int value;
    private Node temp;
    private Set<String> keySet = new HashSet();
    public Solution(int capacity) {
        // write code here
        nodes = new Node[capacity];
    }

    public int get(int key) {
        // write code here
        value = -1;
        if(keySet.contains(String.valueOf(key))) {
            for (int i = 0; i < nodes.length; i++) {
                if (nodes[i].key.equals(String.valueOf(key))) {
                    value = nodes[i].value;
                    temp = nodes[i];
                    for (int j = i; j > 0; j--) {
                        nodes[j] = nodes[j - 1];
                    }
                    nodes[0] = temp;
                    return value;
                }
            }
        } else {
            return -1;
        }
        return value;
    }

    public void set(int key, int value) {
        // write code here
        if(keySet.contains(String.valueOf(key))){
            for (Node node : nodes) {
                if(node!=null&&node.key.equals(String.valueOf(key))){
                    node.value=value;
                    break;
                }
            }
        } else {
            for (int i = nodes.length - 1; i > 0; i--) {
                nodes[i] = nodes[i-1];
            }
            nodes[0]=new Node(String.valueOf(key),value);
            keySet.add(String.valueOf(key));
        }
    }
}

class Node {
    public String key;
    public int value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node() {
    }

    public Node(String key, int value) {
        this.key = key;
        this.value = value;
    }
}
